//
//  View+Extension.swift
//  Game
//
//  Created by woniu on 2025/8/22.
//

import Foundation
import UIKit

extension UIView {

    public var width: CGFloat {
        
        get { return self.frame.size.width }
        set {
              var frame = self.frame
              frame.size.width = newValue
              self.frame = frame
          }
      }

    public  var height: CGFloat {
          get { return self.frame.size.height }
          set {
              var frame = self.frame
              frame.size.height = newValue
              self.frame = frame
          }
      }
      
    public  var size: CGSize  {
          get { return self.frame.size }
          set {
              var frame = self.frame
              frame.size = newValue
              self.frame = frame
          }
      }

     
    public var origin: CGPoint {
          get { return self.frame.origin }
          set {
              var frame = self.frame
              frame.origin = newValue
              self.frame = frame
          }
      }
      
    public  var x: CGFloat {
          get { return self.frame.origin.x }
          set {
              var frame = self.frame
              frame.origin.x = newValue
              self.frame = frame
          }
      }
    public  var y: CGFloat {
          get { return self.frame.origin.y }
          set {
              var frame = self.frame
              frame.origin.y = newValue
              self.frame = frame
          }
      }
      
    public  var centerX: CGFloat {
          get { return self.center.x }
          set {
              self.center = CGPoint(x: newValue, y: self.center.y)
          }
      }
      
    public  var centerY: CGFloat {
          get { return self.center.y }
          set {
              self.center = CGPoint(x: self.center.x, y: newValue)
          }
      }
      
   public   var top : CGFloat {
          get { return self.frame.origin.y }
          set {
              var frame = self.frame
              frame.origin.y = newValue
              self.frame = frame
          }
      }
      
    public  var bottom : CGFloat {
          get { return frame.origin.y + frame.size.height }
          set {
              var frame = self.frame
              frame.origin.y = newValue - self.frame.size.height
              self.frame = frame
          }
      }
      
    public  var right : CGFloat {
          get { return self.frame.origin.x + self.frame.size.width }
          set {
              var frame = self.frame
              frame.origin.x = newValue - self.frame.size.width
              self.frame = frame
          }
      }
      
     
    public var left : CGFloat {
          get { return self.frame.origin.x }
          set {
              var frame = self.frame
              frame.origin.x  = newValue
              self.frame = frame
          }
      }
}

extension UIView{
    /// 指定圆角设置
    /// - view: 需要设置的控件
    /// - corner: 哪些圆角
    /// - radii: 圆角半径
    /// - Returns: layer图层
    /// self.circularBy( corner: [.bottomLeft, .topRight], radii: 5)
    func circularBy(corner: UIRectCorner, radii: CGFloat) {
        let maskPath = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners: corner, cornerRadii: CGSize(width: radii, height: radii))
        let maskLayer = CAShapeLayer.init()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        self.layer.mask  = maskLayer
    }
  
    ///四个角的圆角
    func circular() {
        self.layer.cornerRadius = self.height * 0.5
        self.layer.masksToBounds = true
    }
    
    /// 设置圆角为5
    func circular5() {
        self.layer.cornerRadius = 5
        self.layer.masksToBounds = true
    }
    
    ///设置指定圆角
    func circular(_ height:CGFloat) {
        self.layer.cornerRadius = height
        self.layer.masksToBounds = true
    }
    
    
    /// 设置边框
    /// - Parameters:
    ///   - color: 颜色
    ///   - width: 宽度
    func borderSet(_ color:UIColor?,_ width:Double) {
        self.layer.borderWidth = CGFloat(width)
        self.layer.borderColor = color?.cgColor
    }
    
    ///设置边框颜色
    func setBorder(color:UIColor) {
        self.layer.borderColor = color.cgColor
    }
    
    /// 添加圆角阴影(已测，不要随便修改)
    /// - Parameters:
    ///   - radius: 当前组件圆角
    ///   - colorW: 阴影宽度，偏移量
    ///   - opacity: 阴影透明度
    ///   - shadowRadius: 阴影圆角
    ///    superContentView.addShadow(shadowOffset: CGSize(width: 0, height: 0), opacity: 0.06)
    func addShadow(radius:CGFloat = 5,
                   shadowOffset:CGSize = CGSize(width: 1.5, height: 1.5),
                   opacity:CGFloat = 0.5,
                   shadowRadius:CGFloat = 2,
                   color:UIColor = UIColor.black.withAlphaComponent(0.4)){
        
        self.layer.shadowColor = color.cgColor
        self.layer.shadowOffset = shadowOffset
        self.layer.shadowRadius = shadowRadius
        self.layer.shadowOpacity = Float(opacity)
        self.layer.cornerRadius = radius
    }
}

extension String{
    //去掉所有换行、前后空格、所有回车(不要随便修改，已测试)
    public func replaceRN()->String {
        var str = self.replacingOccurrences(of: "\n", with: "")
        str = str.replacingOccurrences(of: "\r", with: "")
        str = str.removeHeadAndTailSpace
        return str
    }
    
    ///去掉首尾空格
    var removeHeadAndTailSpace:String {
        let whitespace = NSCharacterSet.whitespaces
        return self.trimmingCharacters(in: whitespace)
    }
}
